/**
 * 
 */
package com.ruyipu.forum.model;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.ruyipu.forum.base.Constants;
import com.ruyipu.forum.base.MyLog;
import com.ruyipu.forum.interfaces.IDiscussionModel;
import com.ruyipu.forum.interfaces.IItem;

/**
 * @author donald
 * 
 */
public class DiscussionModel implements IDiscussionModel {
	public static final String TAG = DiscussionModel.class.getSimpleName();
	public static final String NAME = DiscussionModel.class.getName();
	
	private static final String CATEGORIES_URL = Constants.API_URL
			+ "categories/";
	private static final String DISCUSSION_URL = Constants.API_URL
			+ "discussion/index/%s";
	private static final String DISCUSSION_COMMENTS_URL = Constants.API_URL
			+ "discussion/comments/%s/%s/%s";
	private static final String CATEGORY_URL = CATEGORIES_URL + "index/%s/%s/%s";
	
	private static final String ALL_DISCUSSIONS_URL = Constants.API_URL + "discussions/index/%s";
	

	private static DiscussionModel discussionModel;

	private DiscussionModel() {
	}

	public static DiscussionModel getInstance() {
		if (null == discussionModel) {
			discussionModel = new DiscussionModel();
		}
		return discussionModel;
	}
	public List<IItem> getDiscussionsIn(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<IItem> getAnnouncementsIn(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		return null;
	}

	public JSONObject getDiscussionById(Context context, int discussionId) {
		String url = String.format(DISCUSSION_URL, discussionId);
		try {
			return Model.get(context, url);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public JSONObject getDiscussionCommentList(Context context,
			int discussionId, int size, int page) {
		int offset = (page - 1) * size;
		String url = String.format(DISCUSSION_COMMENTS_URL, discussionId, offset,
				size);
		try {
			return Model.get(context, url);
		} catch (UnsupportedEncodingException e) {
			MyLog.e(TAG, e);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public JSONObject loadLocalDiscussionComment(Context context,
			int discussionId, int size, int page) {
		int offset = (page - 1) * size;
		String url = String.format(DISCUSSION_COMMENTS_URL, discussionId, offset,
				size);
		try {
			return Model.loadLocalData(url);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public JSONObject getDiscussionOfCategory(Context context, int categoryId,
			int size, int page) {
		String url = String.format(CATEGORY_URL, categoryId, size, page);
		try {
			return Model.get(context, url);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public JSONObject loadLocalDiscussionOfCategory(int categoryId, int size, int page) {
		String url = String.format(CATEGORY_URL, categoryId, size, page);
		try {
			return Model.loadLocalData(url);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public JSONObject getDiscussions(Context context, int size, int page) {
		String url = String.format(ALL_DISCUSSIONS_URL, page);
		try {
			return Model.get(context, url);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public JSONObject loadLocalDiscussions(int size, int page) {
		String url = String.format(ALL_DISCUSSIONS_URL, page);
		try {
			return Model.loadLocalData(url);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
